<?php
namespace Admin\Controller;
use Tools\AdminController;
class ManagerController extends AdminController {
	function login(){
//		var_dump($_POST);
		//echo date("Y-m-d H:i:sa");
		if(!empty($_POST)){
			
			$manager=new \Model\ManagerModel();
				$info=$manager->check($_POST['name'],$_POST['password']);
				if($info){
				//echo "<script>alert('登录成功！')</script>";
					//给用户信息session持久化操作
					session('admin_name',$info['name']);
					session('admin_id',$info['id']);
					session('loginouttime',$info['loginouttime']);
					//$this->assign("info",$info);
					$loginouttime=date("Y-m-d H:i:s");
					$sql="update manager set loginouttime='$loginouttime'  where id=".$info['id'].";";
					$manager->execute($sql);
					//页面跳转到后台
					//$this->redirect('Index/index',array(),3,' ');
					$this->redirect('Index/index');
				}else{
					echo "<script>alert('用户名或密码错误！')</script>";
					$this->display();
				}
			
		}else{
			$this->display();
		}
	}
	function loginout(){
//		$loginouttime=date("Y-m-d H:i:s a");
//		$manager=new \Model\ManagerModel();
//		$sql="update manager set loginouttime='$loginouttime'  where id=".session('admin_id').";";
//		$manager->execute($sql);
		session(null);//清空session
		$this->redirect('login');
	}
	function showlist(){
		//$info=D('Manager')->where("id!=1")->select();
		//var_dump($infos);
		//查找所有角色，填入下拉框
		$roles=D('Role')->field('name')->select();
		$manager=new \Model\ManagerModel();
		//分页相关
		$total=$manager->count()-1;
		$per=2;
		//实例化分页类对象
		$page_obj=new \Tools\CssPage($total,$per);
		$sql="select * from manager where id!=1 order by id desc ".$page_obj->limit;
		$info=$manager->query($sql);
		$pagelist=$page_obj->fpage(array(3,4,5,6,7,8));
		$this->assign('total',$total);
		$this->assign('pagelist',$pagelist);
		$this->assign('roles',$roles);
		//var_dump($roles);
		$this->assign("info",$info);
		$this->display();
	}
	function addmanager(){
		$manager=new \Model\ManagerModel();
			if(!empty($_POST)){//补充添加时间和角色id值
				$_POST['creattime']=date("Y-m-d H:i:s");
				$roleid=D('Role')->field('id')->where("name='".$_POST['rolename']."'")->find();
				$_POST['roleid']=$roleid['id'];
				//var_dump($_POST);
				if($manager->add($_POST)){//添加成功
					$this->success('Index/success');
					$this->redirect('Manager/showlist',array(),3," ");
				}else{
					$this->error('Index/error');
					$this->redirect('Manager/addmanager',array(),3," ");
				}
				
			}else{
				$roles=D('Role')->field('name')->select();
				$this->assign('roles',$roles);
				$this->display();
			}
	
	}
	function deletemanager($id){
		if(D('Manager')->delete($id)){
			$this->success('Index/success');
			$this->redirect('Manager/showlist',array(),3," ");
		}else{
			$this->error('Index/error');
			$this->redirect('Manager/showlist',array(),3," ");
		}
	}
	
}